package com.bnz.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bnz.page.entity.TbGoods;
import com.bnz.page.entity.TbGoodsDesc;
import com.bnz.page.entity.TbItem;
import com.bnz.page.mapper.TbGoodsDescMapper;
import com.bnz.page.mapper.TbGoodsMapper;
import com.bnz.page.mapper.TbItemCatMapper;
import com.bnz.page.mapper.TbItemMapper;
import com.bnz.page.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private SpringTemplateEngine templateEngine;
    @Autowired
    private TbGoodsMapper goodsMapper;
    @Autowired
    private TbGoodsDescMapper goodsDescMapper;
    @Autowired
    private TbItemMapper itemMapper;
    @Autowired
    private TbItemCatMapper itemCatMapper;

    @Override
    public void createHtml(String goodsId) throws IOException {

        // 1.1 定义模板工作的上下文环境
        Context context = new Context();
        //1.2 设置当前上下文数据
        Map<String, Object> maps = new HashMap<>();
        //1.2.1 将goods表的数据放入maps中
        TbGoods goodsEntity = goodsMapper.selectById(goodsId);
        //1.2.2 将数据放入maps中
        maps.put("goods", goodsEntity);
        //1.3.1 查出goodscat表中的数据放入maps中
        TbGoodsDesc goodsDesc = goodsDescMapper.selectById(goodsId);
        maps.put("goodsDesc", goodsDesc);
        maps.put("itemImages", JSON.parseArray(goodsDesc.getItemImages()));
        maps.put("customAttributeItems", JSON.parseArray(goodsDesc.getCustomAttributeItems()));
        maps.put("specificationItems", JSON.parseArray(goodsDesc.getSpecificationItems()));

        //1.4 查询sku商品列表
        List<TbItem> items = itemMapper.selectList(new QueryWrapper<TbItem>().eq("goods_id", goodsId).orderByDesc("is_default"));
        maps.put("items", items);
        if (items.size() > 0) {
            maps.put("item", items.get(0));
        }
        //1.5 查询商品分类
        String caregory1Name = itemCatMapper.selectById(goodsEntity.getCategory1Id()).getName();
        String caregory2Name = itemCatMapper.selectById(goodsEntity.getCategory2Id()).getName();
        String caregory3Name = itemCatMapper.selectById(goodsEntity.getCategory3Id()).getName();
        maps.put("caregory1Name",caregory1Name);
        maps.put("caregory2Name",caregory2Name);
        maps.put("caregory3Name",caregory3Name);

        //2.1 定义文件输出流 参数: 文件路径 + 文件名称+ 文件类型
        Writer writer = new FileWriter("E:/Desktop/folderApp/nginx-1.10.3/html/item/" + goodsId + ".html");


        //参数1：代表模板的逻辑视图名
		//参数2：代表模板引擎的上下文环境
		//参数3：代表要将模板文件写出的输出流对象
        context.setVariables(maps);
        templateEngine.process("item", context, writer);

    }
}
